﻿package com.crl.ui.controls.buttons
{
	import com.crl.core.IDataRenderer;
	
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author crl
	 */
	public class Button extends EventDispatcher implements IDataRenderer
	{
		protected var state:StateButton;
		protected var skin:MovieClip;
		protected var _overEnabled:Boolean=true;
		protected var _enabled:Boolean=true;
		protected var _selected:Boolean = false;
		
		public var name:String="baseButton";
		protected var _toggle:Boolean=false;
		public function Button(mc:MovieClip) 
		{
			this.state = this.getState(mc);
			this.skin = state.getView();
			this.skin.mouseChildren=false;
			this.skin.buttonMode=true;
			this.enabled = true;
		}
		
		protected function getState(mc:MovieClip):StateButton{
			return new StateButton(mc);
		}
		
		/**
		 * 添加鼠标经过事件;
		 */
		public function set overEnabled(bool:Boolean):void{
			_overEnabled=bool;
			if(_overEnabled){
				this.skin.addEventListener(MouseEvent.ROLL_OVER,overStateHandler,false,0,true);
				this.skin.addEventListener(MouseEvent.ROLL_OUT,overStateHandler,false,0,true);
			}else{
				this.skin.removeEventListener(MouseEvent.ROLL_OVER,overStateHandler);
				this.skin.removeEventListener(MouseEvent.ROLL_OUT,overStateHandler);
			}
		}
		private function overStateHandler(event:MouseEvent):void {
			if(this.selected || this.enabled==false)return;
			this.state.over(event.type == MouseEvent.ROLL_OVER);
		}
		
		/**
		 * 添加鼠标事件;
		 */
		protected function set clickEnabled(bool:Boolean):void {
			if (bool) {
				if(_toggle ==false){
					this.skin.addEventListener(MouseEvent.MOUSE_DOWN, donwupHandler);
					this.skin.addEventListener(MouseEvent.MOUSE_UP, donwupHandler);
				}
				this.skin.addEventListener(MouseEvent.CLICK, clickHandler);
			}else {
				if(_toggle==false){
					this.skin.removeEventListener(MouseEvent.MOUSE_DOWN, donwupHandler);
					this.skin.removeEventListener(MouseEvent.MOUSE_UP, donwupHandler);
				}
				this.skin.removeEventListener(MouseEvent.CLICK, clickHandler);
			}
		}
		
		private var stage:Stage;
		protected function donwupHandler(event:MouseEvent):void {
			if (event.type == MouseEvent.MOUSE_DOWN) {
				this.state.select(true);
				this.stage = this.skin.stage;
				this.stage.addEventListener(MouseEvent.MOUSE_UP, donwupHandler,false,0,true);
			}else {
				//this.stage.removeEventListener(MouseEvent.MOUSE_UP, donwupHandler);
				if(event.currentTarget==this.skin){
					event.stopPropagation()
					this.state.over(true);
				}else{
					if(event.currentTarget==this.stage && this.enabled==false)return;
					this.state.select(false);
				}
			}
		}
		
		public function set enabled(bool:Boolean):void {
			this._enabled = bool;
			this.state.enabled(bool);
			
			this.overEnabled=bool;
			this.clickEnabled=bool;
		}
		public function get enabled():Boolean {
			return this._enabled;
		}
		
		
		protected function clickHandler(event:MouseEvent):void {
			if(this.enabled==false)return;
			this.dispatchEvent(new MouseEvent(MouseEvent.CLICK))
		}
		
		
		public function set selected(bool:Boolean):void {
			this._selected = bool;
			this.state.select(bool);
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		public function get selected():Boolean {
			return this._selected;
		}
		
		public function getView():MovieClip{
			return this.skin;
		}
		
		private var _data:Object
		/**
		 *  The data to render or edit.
		 */
		public function get data():Object{
			return this._data;
		}
		
		/**
		 *  @private
		 */
		public function set data(value:Object):void{
			this._data=value;
		}
		
	}
	
}